package com.sinocare.project.maintenance.controller;


import cn.hutool.core.date.DateUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sinocare.project.common.entity.*;
import com.sinocare.project.common.util.JwtTokenUtil;
import com.sinocare.project.common.util.WebContextUtil;
import com.sinocare.project.maintenance.realm.MyRealm;
import com.sinocare.project.maintenance.service.SccMenuService;
import com.sinocare.project.common.result.Result;
import com.sinocare.project.common.result.ResultCode;
import com.sinocare.project.maintenance.service.SccRoleMenuService;
import com.sinocare.project.maintenance.service.SccUserRoleService;
import com.sinocare.project.maintenance.util.JsonList;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单权限表(SccMenu)表控制层
 *
 * @author makejava
 * @since 2021-12-21 14:39:55
 */
@RestController
@RequestMapping("sccMenu")
@CrossOrigin
public class SccMenuController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SccMenuService sccMenuService;

    @Resource
    private SccRoleMenuService sccRoleMenuService;

    @Resource
    private SccUserRoleService sccUserRoleService;

    /**
     * 分页查询所有父级菜单
     *
     * @return 所有数据
     */
    @GetMapping
    public Result selectPageAll(Integer page, Integer limit) {
        QueryWrapper<SccMenu> wrapper = new QueryWrapper<>();
        wrapper.ne("p_id", -1);
        wrapper.eq("status", 1);
        PageHelper.startPage(page, limit);
        List<SccMenu> list = this.sccMenuService.list(wrapper);
        PageInfo<SccMenu> info = new PageInfo<>(list);
        return Result.ok(ResultCode.SUCCESS, info.getTotal(), info.getList());
    }


    /**
     * 查询所有父级菜单
     *
     * @return 所有数据
     */
    @GetMapping("/no")
    public Result selectAll() {
        QueryWrapper<SccMenu> wrapper = new QueryWrapper<>();
        wrapper.select("menu_id,menu_name");
        wrapper.eq("p_id", 0);
        wrapper.eq("status", 1);
        List<SccMenu> list = this.sccMenuService.list(wrapper);
        return Result.ok(ResultCode.SUCCESS, list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result selectOne(@PathVariable Serializable id) {
        return Result.ok(ResultCode.SUCCESS, this.sccMenuService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sccMenu 实体对象
     * @return 新增结果
     */
    @PostMapping
    public Result insert(@RequestBody SccMenu sccMenu) {
        sccMenu.setSeq(0);
        sccMenu.setStatus(1);
        sccMenu.setCreateTime(DateUtil.date());
        sccMenu.setUpdateTime(DateUtil.date());
        return Result.ok(ResultCode.SUCCESS, this.sccMenuService.save(sccMenu));
    }

    /**
     * 修改数据
     *
     * @param sccMenu 实体对象
     * @return 修改结果
     */
    @PutMapping
    public Result update(@RequestBody SccMenu sccMenu) {
        sccMenu.setUpdateTime(DateUtil.date());
        return Result.ok(ResultCode.SUCCESS, this.sccMenuService.updateById(sccMenu));
    }

    /**
     * 根据id修改数据状态
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Serializable id) {
        UpdateWrapper<SccMenu> wrapper = new UpdateWrapper<>();
        wrapper.eq("menu_id", id).set("status", 0);
        wrapper.set("update_time", DateUtil.date());
        return Result.ok(ResultCode.SUCCESS, this.sccMenuService.update(null, wrapper));
    }
    @GetMapping("verify")
    public Result verify(String token){
        try {
            JwtTokenUtil.verify(token);
            return Result.ok(ResultCode.SUCCESS,JwtTokenUtil.getToken(token));
        }catch (Exception e){
            return Result.fail(ResultCode.FAIL,false);
        }
    }
    /**
     * 初始化菜单
     *
     * @return
     */

    @GetMapping(value = "init")
//    @RequiresRoles(value = {"admin"})
//    @RequiresAuthentication
    public Map<String, Object> list(String token) {
            JWTResult jwt = JwtTokenUtil.getToken(token);
            QueryWrapper<SccUserRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", jwt.getUserId());
            SccUserRole one = sccUserRoleService.getOne(queryWrapper);

            QueryWrapper<SccRoleMenu> query = new QueryWrapper<>();
            query.eq("role_id", one.getRoleId());
            List<SccRoleMenu> roleMenuList = sccRoleMenuService.list(query);

            QueryWrapper<SccMenu> wrapper = new QueryWrapper<>();
            wrapper.eq("status", 1);
            List<SccMenu> list = this.sccMenuService.list(wrapper);
            return JsonList.getJson(list, roleMenuList);
    }

    /**
     * 初始化角色id获取角色权限
     *
     * @return
     */
    @GetMapping(value = "setRole/{id}")
    public Result setMenu(@PathVariable Serializable id) {
        QueryWrapper<SccRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", id);
        queryWrapper.select("menu_id");
        List<SccRoleMenu> roleMenuList = sccRoleMenuService.list(queryWrapper);
        QueryWrapper<SccMenu> wrapper = new QueryWrapper<>();
        wrapper.ne("p_id", -1);
        wrapper.eq("status", 1);
        List<SccMenu> list = this.sccMenuService.list(wrapper);
        List menu = JsonList.setMenu(list, roleMenuList);
        return Result.ok(ResultCode.SUCCESS, Long.parseLong(list.size() + ""), menu);
    }

}

